<template>
  <div class="result-card-out">
    <div v-if="data && data.content && data.content.length > 0">
      <div>
        <div
          class="card-item"
          v-for="(item, index) in data.content"
          :key="item.id"
        >
          <mu-flex justify-content="between" class="card-item-header">
            <mu-flex align-items="center">
              <mu-avatar size="50" class="user-avatar">
                <img :src="baseImgUrl + item.userCover" />
              </mu-avatar>
              <div>
                <div class="user-name">{{ item.userName }}</div>
                <span class="create-time">发布于：{{ item.sendTime }}</span>
              </div>
            </mu-flex>
            <img
              src="https://image.xuexiwangzhe.com/Fnhib436IYmXPnDBMF6ShXfC10zQ"
              class="card-item-header-top-img"
              v-show="item.isStick"
            />
          </mu-flex>
          <div class="card-item-content">
            <div class="item-content-main">
              <assess
                :data="item"
                v-if="data.themeData.itemType === 12"
              ></assess>
              <homework
                :data="item"
                v-if="data.themeData.itemType === 11"
              ></homework>
              <exam :data="item" v-if="data.themeData.itemType === 13"></exam>
              <punch :data="item" v-if="data.themeData.itemType === 10"></punch>
            </div>
            <mu-flex
              :class="[
                'operate',
                item.supportNum === 0 && item.commentNum === 0
                  ? 'operate-nodata'
                  : '',
              ]"
              align-items="center"
              justify-content="between"
            >
              <span class="all-comment">全部评论（{{ item.commentNum }}）</span>
              <mu-flex align-items="center">
                <mu-button flat class="operate-item" @click="praiseClick(item)">
                  <img
                    class="operate-item-img"
                    v-if="item.isPraise"
                    src="https://image.xuexiwangzhe.com/FhTEPtJC_R1qKBcWEPJD2E2LnaLJ"
                  />
                  <img
                    class="operate-item-img"
                    v-else
                    src="https://image.xuexiwangzhe.com/FtV92dzYzX8l77b07siybwN34t7A"
                  />
                  <span>已赞（{{ item.supportNum }}）</span>
                </mu-button>
                <a-popover
                  placement="bottom"
                  arrow-point-at-center
                  trigger="click"
                  v-model="commentOpen[item.id]"
                >
                  <div slot="content" class="comment-panel">
                    <mu-flex class="content">
                      <div class="title">点评</div>
                      <a-textarea
                        :autoSize="{ minRows: 4, maxRows: 4 }"
                        v-model="commentContent[item.id]"
                        :max-length="1000"
                      ></a-textarea>
                    </mu-flex>
                    <mu-flex class="operate" align-items="center">
                      <mu-button
                        class="submit-btn"
                        :disabled="!commentContent[item.id]"
                        @click="submitComment(item)"
                        >提交点评</mu-button
                      >
                      <mu-button
                        class="cancel-btn"
                        flat
                        @click="cancelComment(item)"
                        >取消</mu-button
                      >
                    </mu-flex>
                  </div>
                  <mu-button flat class="operate-item">
                    <img
                      class="operate-item-img"
                      src="https://image.xuexiwangzhe.com/FjQYDAoZjEJ_OqaWUtwu_YUXMSe5"
                    />
                    <span>点评</span>
                  </mu-button>
                </a-popover>
                <mu-button
                  v-if="
                    data.themeData.itemType === 10 ||
                      data.themeData.itemType === 11
                  "
                  flat
                  class="operate-item"
                  @click="stick(item)"
                >
                  <img
                    class="operate-item-img operate-item-img-top"
                    src="https://image.xuexiwangzhe.com/Fm3CIodByIuBEahYxPO7n8e_Hhmi"
                  />
                  <span v-if="item.isStick">取消优秀</span>
                  <span v-else>优秀</span>
                </mu-button>
                <mu-button
                  v-if="
                    data.themeData.itemType === 10 ||
                      data.themeData.itemType === 11
                  "
                  flat
                  class="operate-item"
                  @click="deleteData(item, index)"
                >
                  <img
                    class="operate-item-img"
                    src="https://image.xuexiwangzhe.com/Fu7tcRa1YEKWWpMsQme8LZW3Yyht"
                  />
                  <span>删除</span>
                </mu-button>
              </mu-flex>
            </mu-flex>
            <mu-divider
              v-if="item.praiseLogs"
              class="item-divider"
            ></mu-divider>
            <mu-flex
              class="have-praise"
              v-if="item.praiseLogs"
              align-items="center"
            >
              <img
                src="https://image.xuexiwangzhe.com/FlDlnacn3kJivD2PwAzbtZnUyfAa"
                class="have-praise-img"
              />
              <span class="have-praise-content">{{
                item.praiseLogs | praise
              }}</span>
            </mu-flex>
            <mu-divider
              v-if="item.commentNum > 0"
              class="item-divider"
            ></mu-divider>
            <div v-if="item.commentNum > 0">
              <div
                :class="[
                  item.commentNum > 3 ? 'comment-more-main' : '',
                  commentMore[item.id] ? 'comment-more-main-open' : '',
                ]"
              >
                <mu-flex
                  class="comment-item"
                  v-if="item.lecturerComments"
                  v-for="(childItem, childIndex) in item.lecturerComments"
                  :key="childItem.id"
                >
                  <mu-flex class="comment-tag" align-items="center">
                    <img
                      src="https://image.xuexiwangzhe.com/FlDkYhMec7RcM3DASQuYFX-usAYy"
                      class="comment-tag-img"
                    />
                  </mu-flex>
                  <div class="comment-item-main">
                    <mu-flex align-items="center" class="comment-header">
                      <mu-avatar size="40" class="comment-avatar">
                        <img :src="baseImgUrl + childItem.userCover" />
                      </mu-avatar>
                      <div>
                        <div class="comment-name">
                          {{ childItem.userName }}
                        </div>
                        <span class="comment-time"
                          >点评于 {{ childItem.subTime }}</span
                        >
                      </div>
                    </mu-flex>
                    <div class="comment-content">{{ childItem.content }}</div>
                    <div class="comment-item-operate">
                      <a @click="commentDelete(item, childItem, childIndex)"
                        >删除</a
                      >
                    </div>
                    <mu-divider class="item-divider"></mu-divider>
                  </div>
                </mu-flex>
                <mu-flex
                  class="comment-item"
                  v-if="item.comments"
                  v-for="(childItem, childIndex) in item.comments"
                  :key="childItem.id"
                >
                  <mu-flex
                    class="comment-tag comment-tag-normal"
                    align-items="center"
                  >
                    <img
                      src="https://image.xuexiwangzhe.com/FlDkYhMec7RcM3DASQuYFX-usAYy"
                      class="comment-tag-img"
                    />
                  </mu-flex>
                  <div class="comment-item-main">
                    <mu-flex align-items="center" class="comment-header">
                      <mu-avatar size="40" class="comment-avatar">
                        <img :src="baseImgUrl + childItem.userCover" />
                      </mu-avatar>
                      <div>
                        <div class="comment-name">{{ childItem.userName }}</div>
                        <span class="comment-time"
                          >点评于 {{ childItem.subTime }}</span
                        >
                      </div>
                    </mu-flex>
                    <div class="comment-content">{{ childItem.content }}</div>
                    <div class="comment-item-operate">
                      <a @click="commentDelete(item, childItem, childIndex)"
                        >删除</a
                      >
                    </div>
                    <mu-divider class="item-divider"></mu-divider>
                  </div>
                </mu-flex>
              </div>
              <div
                class="comment-more"
                v-if="item.commentNum > 3 && !commentMore[item.id]"
              >
                <a @click="commentMoreControl(item, true)">查看更多</a>
              </div>
              <div
                class="comment-more"
                v-if="commentMore[item.id] && item.commentNum > 3"
              >
                <a @click="commentMoreControl(item, false)">收起</a>
              </div>
            </div>
          </div>
        </div>
      </div>
      <mu-flex
        justify-content="center"
        v-if="dataCount > 10"
        style="margin: 32px 0;"
      >
        <mu-pagination
          :total="dataCount"
          :current.sync="pageControl"
          :page-size="10"
          @change="getData"
        ></mu-pagination>
      </mu-flex>
    </div>
    <div class="loading" v-else-if="loading">
      <mu-circular-progress :size="36"></mu-circular-progress>
    </div>
    <div class="no-content" v-else>
      暂无数据
    </div>
  </div>
</template>

<script>
import assess from "./tools/assess.vue";
import exam from "./tools/exam.vue";
import homework from "./tools/homework.vue";
import punch from "./tools/punch.vue";

import Message from "muse-ui-message";
export default {
  props: ["data", "loading", "page"],
  filters: {
    time(value) {
      if (!value) return "";
      return value.replace(/\//g, "-");
    },
    praise(value) {
      if (!value) return "";
      for (let j = 0; j < value.length; j++) {
        value[j] = unescape(value[j].replace(/\\/g, "%"));
      }
      return value.join("、");
    },
  },
  data() {
    return {
      baseImgUrl: "https://image.xuexiwangzhe.com/",
      commentOpen: {},
      commentContent: {},
      commentMore: {},
    };
  },
  components: {
    assess,
    exam,
    homework,
    punch,
  },
  computed: {
    dataCount() {
      return this.data.totalElements;
    },
    pageControl: {
      set(val) {
        this.$emit("update:page", val);
      },
      get() {
        return this.page;
      },
    },
  },
  methods: {
    getData() {
      this.$emit("getData");
    },
    praiseClick(item) {
      const params = {
        itemType: this.data.themeData.itemType,
        id: item.id,
        projectId: this.data.themeData.projectId,
        moduleId: this.data.themeData.id,
        SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
      };
      this.$axios.post("/praiselog/praise", params).then((res) => {
        if (res.success) {
          if (item.isPraise === 0) {
            item.isPraise = 1;
            item.supportNum += 1;
          } else {
            item.isPraise = 0;
            item.supportNum -= 1;
          }

          if (res.praiseUserNameList) {
            item.praiseLogs = res.praiseUserNameList;
          } else {
            item.praiseLogs = "";
          }
        }
      });
    },
    cancelComment(item) {
      this.commentContent[item.id] = "";
      this.commentOpen[item.id] = false;
    },
    submitComment(item) {
      if (!this.commentContent[item.id]) {
        return;
      }
      const params = {
        itemType: this.data.themeData.itemType,
        id: item.id,
        projectId: this.data.themeData.projectId,
        moduleId: this.data.themeData.id,
        content: this.commentContent[item.id],
        replyId: 0,
        replyName: 0,
        weight: 0,
        SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
      };
      this.$axios.post("/comment/comment", params).then((res) => {
        if (res.success) {
          this.$toast.success("评论成功");
          let comment = res.comment;
          comment.roleId = 2;
          if (item.lecturerComments) {
            item.lecturerComments.push(comment);
          } else {
            item.lecturerComments = [comment];
          }
          if (item.commentNum) {
            item.commentNum += 1;
          } else {
            item.commentNum = 1;
          }
          this.commentContent[item.id] = "";
          this.commentOpen[item.id] = false;
        } else {
          this.$toast.error(res.msg);
        }
      });
    },
    commentMoreControl(item, value) {
      this.$set(this.commentMore, item.id, value);
    },
    commentDelete(item, commentItem, index) {
      const params = {
        id: commentItem.id,
        projectId: this.data.themeData.projectId,
        moduleId: this.data.themeData.id,
        SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
      };
      this.$axios.post("/comment/delete", params).then((res) => {
        if (res.success) {
          if (
            commentItem.roleId !== 1 &&
            commentItem.roleId !== 2 &&
            commentItem.roleId !== 3
          ) {
            item.comments.splice(index, 1);
          } else {
            item.lecturerComments.splice(index, 1);
          }
          item.commentNum -= 1;
          this.$toast.success("删除成功");
        } else {
          this.$toast.error(res.msg);
        }
      });
    },
    stick(item) {
      Message.confirm("确定设为优秀么？", "提示", {
        type: "info",
      }).then(({ result }) => {
        if (result) {
          const params = {
            id: item.id,
            projectId: this.data.themeData.projectId,
            moduleId: this.data.themeData.id,
            SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
          };
          let url = "/clockcontent/stick";
          if (this.data.themeData.itemType === 11) {
            url = "/previewwork/stick";
          }
          this.$axios.post(url, params).then((res) => {
            if (res.success) {
              if (item.isStick) {
                item.isStick = 0;
                this.$toast.success("已取消优秀");
              } else {
                item.isStick = 1;
                this.$toast.success("已设为优秀");
              }
            } else {
              this.$toast.error(res.msg);
            }
          });
        }
      });
    },
    deleteData(item, index) {
      Message.confirm("确定要删除么？", "提示", {
        type: "warning",
      }).then(({ result }) => {
        if (result) {
          const params = {
            id: item.id,
            projectId: this.data.themeData.projectId,
            moduleId: this.data.themeData.id,
            SESSIONKEY: sessionStorage.getItem("SESSIONKEY"),
          };
          let url = "/clockcontent/delete";
          if (this.data.themeData.itemType === 11) {
            url = "/previewwork/delete";
          }
          this.$axios.post(url, params).then((res) => {
            if (res.success) {
              this.data.content.splice(index, 1);
              this.$toast.success("删除成功");
            } else {
              this.$toast.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="less">
.comment-panel {
  width: 300px;
  padding: 10px;

  .content {
    margin-bottom: 20px;
  }

  .title {
    width: 50px;
  }
  .operate {
    padding-left: 40px;
  }

  .submit-btn {
    color: white;
    background-color: #3567ff;
    box-shadow: none;
  }

  .cancel-btn {
    color: #9f9f9f;
  }
}
.result-card-out {
  .loading {
    padding-top: 10px;
    text-align: center;
  }
  .no-content {
    padding-top: 60px;
    padding-bottom: 60px;
    color: #666;
    text-align: center;
  }
  .card-item {
    padding: 20px 40px 10px 40px;
    border-bottom: 4px solid #f6f6f6;
    &:last-child {
      border-bottom: none;
    }
  }
  .card-item-header {
    margin-bottom: 20px;
  }

  .card-item-header-top-img {
    width: 40px;
  }
  .card-item-content {
    padding-left: 65px;
  }
  .user-avatar {
    margin-right: 15px;
  }

  .user-name {
    font-size: 16px;
    color: #475471;
    margin-bottom: 5px;
  }

  .create-time {
    color: #acacac;
  }

  .item-divider {
    background-color: #ededed;
  }

  .item-content-main {
    margin-bottom: 15px;
  }

  .item-content {
    margin-bottom: 10px;
  }

  .operate {
    margin-bottom: 5px;
  }

  .operate-nodata {
    margin-bottom: 0;
  }

  .all-comment {
    color: #aaa;
  }

  .operate-item {
    &:last-child {
      margin-right: 0;
    }
    margin-right: 5px;
    &-img {
      width: 20px;
      margin-right: 10px;
    }

    &-img-top {
      width: 12px;
    }
  }

  .have-praise {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .have-praise-img {
    width: 20px;
    margin-right: 10px;
  }

  .have-praise-content {
    color: #5a6e96;
  }

  .comment-more-main {
    height: 327px;
    overflow-y: hidden;
  }

  .comment-more-main-open {
    height: auto;
  }

  .comment-item {
    &:hover {
      background-color: #f2f2f5;
      .comment-item-operate {
        display: block;
      }
    }
    position: relative;
    width: 800px;
    padding-top: 20px;

    .comment-item-operate {
      position: absolute;
      top: 50%;
      right: 20px;
      margin-top: -10.5px;
      display: none;
      a {
        color: #000;
        cursor: pointer;
      }
    }
  }

  .comment-item-main {
    flex: 1;
  }

  .comment-header {
    margin-bottom: 10px;
  }

  .comment-tag {
    height: 50px;
  }

  .comment-tag-normal {
    visibility: hidden;
  }

  .comment-tag-img {
    width: 30px;
    margin-right: 15px;
  }

  .comment-avatar {
    margin-right: 10px;
  }

  .comment-name {
    color: #4e597c;
    margin-bottom: 5px;
  }

  .comment-time {
    color: #acacac;
  }

  .comment-content {
    color: #4f4f4f;
    padding-left: 50px;
    margin-bottom: 20px;
  }

  .comment-more {
    padding-top: 20px;
    text-align: center;
  }
}
</style>
